package com.cms.service;

import java.util.List;

import com.cms.model.ChannelPercent;
import com.service.AbstractDateService;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class ChannelPercentServiceImpl extends AbstractDateService implements ChannelPercentService {

	@Override
	public boolean saveChannelPercent(ChannelPercent model) {
		String sql="";
		if(getChannelPercentByContent(model)!=null){
			 sql = "update t_channelpercent set daylimit = "+model.getDaylimit()+
					",daypercent = "+model.getDaypercent()+" where appnumber='"+model.getAppnumber()+"' and appversionnumber='"+model.getAppversionnumber()+"' and channelid="+model.getChannelid();
		}else{
			model.setId(getTableId("t_channelpercent"));
			sql = "insert into t_channelpercent (id, channelid,appnumber,appversionnumber, daylimit," +
					" daypercent, createuser, createtime) " +
					"values ("+model.getId()+", "+model.getChannelid()+
					",'"+model.getAppnumber()+"', '"+model.getAppversionnumber()+"',"+model.getDaylimit()+", "+model.getDaypercent()+", '"+model.getCreateuser()+"', '"+model.getCreatetime()+"')";
		}
		logger.info("[SQL] [method:saveChannelPercent] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean deleteChannelPercent(ChannelPercent model) {
		String sql = "delete from t_channelpercent where id="+model.getId();
		logger.info("[SQL] [method:deleteChannelPercent] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public boolean updateChannelPercent(ChannelPercent model) {
		String sql = "update t_channelpercent set daylimit = "+model.getDaylimit()+
		",daypercent = "+model.getDaypercent()+",appnumber='"+model.getAppnumber()+"',appversionnumber='"+model.getAppversionnumber()+"',channelid="+model.getChannelid()+" where id= "+model.getId();
		logger.info("[SQL] [method:updateChannelPercent] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base,sql);
	}

	@Override
	public List<ChannelPercent> getChannelPercent() {
		String sql = "select * from t_channelpercent order by id desc";
		logger.info("[SQL] [method:getChannelPercent] ["+sql+"]");
		ModelGenerator<ChannelPercent> model = new ModelGenerator<ChannelPercent>(ChannelPercent.class);
		return model.generateList(base,sql.toString());
	}

	@Override
	public ChannelPercent getChannelPercent(ChannelPercent info) {
		String sql = "select * from t_channelpercent where id="+info.getId();
		logger.info("[SQL] [method:getChannelPercent] ["+sql+"]");
		ModelGenerator<ChannelPercent> model = new ModelGenerator<ChannelPercent>(ChannelPercent.class);
		return model.generate(base,sql.toString());
	}

	@Override
	public ChannelPercent getChannelPercentByContent(ChannelPercent model) {
		String sql="select * from t_channelpercent where channelid="+model.getChannelid()+" and appnumber='"+model.getAppnumber()+"' and appversionnumber='"+model.getAppversionnumber()+"'";
		ModelGenerator<ChannelPercent> m = new ModelGenerator<ChannelPercent>(ChannelPercent.class);
		return m.generate(base,sql.toString());
	}

}
